package week7;

import week6.BinaryTreeNode;

public class AVLTreeNode<T> extends BinaryTreeNode<T> {
    public T element;
    public int height;
    public AVLTreeNode<T> left;
    public AVLTreeNode<T> right;

    public AVLTreeNode(T e)
    {
        super(e);
    }

    public AVLTreeNode(T obj, AVLTreeNode<T> left, AVLTreeNode<T> right,int height)
    {
        super(obj);
        this.height = height;
        if (left == null)
            this.left = null;
        else
            this.left = left;

        if (right == null)
            this.right = null;
        else
            this.right = right;

    }

    // 判断以该结点为树根的子树是否需要平衡
    public boolean isBalanced() {
        int leftHeight = 0;
        if (left != null)
        {
            leftHeight = left.height;
        }

        int rightHeight = 0;
        if (right != null)
        {
            rightHeight = right.height;
        }

        return Math.abs(leftHeight - rightHeight) <= 1;
    }

    public T getElement() {
        return element;
    }

    public AVLTreeNode<T> getLeft() {
        return left;
    }

    public AVLTreeNode<T> getRight() {
        return right;
    }

    public void setLeft(AVLTreeNode<T> left) {
        this.left = left;
    }

    public void setRight(AVLTreeNode<T> right) {
        this.right = right;
    }

    public String toString() {
        return "Element:" + element + " Height:" + height + " Balanced:" + isBalanced();
    }



}
